/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.node;

//import im.general.AbstractListenBehaviour;
import im.general.AbstractListenOnceBehaviour;
import im.general.Config;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gnaskky
 */
public class NewCHNotifyListenerBehaviour extends AbstractListenOnceBehaviour {

    private MobileNodeAgent mn;

    public NewCHNotifyListenerBehaviour(MobileNodeAgent mn) throws Exception {
        this.mn = mn;
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.PROPAGATE );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.NEW_CH_ADVERTISE_MSG);
        mt = MessageTemplate.MatchCustom(wrap_msg, true);
    }

    @Override
    public void handleMessage( ACLMessage in_msg ) {
        try {
            //if( Config.TRACE_LVL > Config.ULTRA_TR_LVL )
            System.out.println("[NewCHNotifyListener]\tNEW_CH_ADVERTISE_MSG [MN CH]     " + mn.getLocalName() 
                    + "\t" + in_msg.getSender().getLocalName() + "\t" + in_msg.getUserDefinedParameter(Config.NEW_CH_CONTAINER ) + "\tMN: " + this.mn.getClusterHeadID() + " " + this.mn.getContainerController().getContainerName() );

            if( in_msg.getUserDefinedParameter(Config.NEW_CH_CONTAINER ).equals(this.mn.getContainerController().getContainerName()) )
                this.mn.gotCHpromotion = true;
            
            /* build a new vector for CH selection */
            Hashtable<String,Double> newCHs = new Hashtable<String,Double>();
            newCHs.put(mn.getClusterHeadID(), mn.getClusterHeadIdx());
            newCHs.put(in_msg.getUserDefinedParameter(Config.NEW_CH_NAME),
                    Double.valueOf(in_msg.getUserDefinedParameter(Config.NEW_CH_IDX)));

            mn.setCHpromotion(in_msg.getUserDefinedParameter(Config.NEW_CH_NAME));

            mn.setCHs(newCHs);


            this.mn.resetNewChHnld();
            /* go next behaviour */
            this.setDone();
            

        } catch (Exception ex) {
            Logger.getLogger(NewCHNotifyListenerBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

}


/*
public class NewCHNotifyListenerBehaviour extends SimpleBehaviour {

    MobileNodeAgent mn;
    MessageTemplate mt;
    boolean finished = false;

    / ** Creates a new instance of EventServiceListen * /

    public NewCHNotifyListenerBehaviour(MobileNodeAgent mn) throws Exception {
        this.mn = mn;
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.PROPAGATE );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.NEW_CH_ADVERTISE_MSG);
        mt = MessageTemplate.MatchCustom(wrap_msg, true);
    }

    public void action() {
        try {
            ACLMessage in_msg = null;

            in_msg = mn.receive(mt);

            if(in_msg != null) {
                if( Config.TRACE_LVL > Config.ULTRA_TR_LVL )
                    System.out.println("[NewCHNotifyListener]\tNEW_CH_ADVERTISE_MSG from:\t" + in_msg.getSender().getName() );

                // build a new vector for CH selection
                Vector<String> newCHs = new Vector<String>();
                newCHs.add(mn.getClusterHeadID());
                newCHs.add(in_msg.getUserDefinedParameter(Config.NEW_CH_NAME));

                mn.setCHs(newCHs);

                // go next behaviour
                finished = true;
            } else
                block();

        } catch (Exception ex) {
            Logger.getLogger(NewCHNotifyListenerBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

    }


    @Override
    public boolean done() {
        return finished;
    }

}
*/
